<?php
/**
 * 
 */
class Back_AdminController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction() {
        $statistic = new Back_Model_Statistic();
        
        $active = 1; $notConfirmed = 2; $blocked = 3;
        /*
         * Advertisers
         */        
        $countAdvertisers = $statistic->getCountAllAdvertisers();
        $this->view->countAdvertisers = $countAdvertisers;
        
        $countActiveAdvertisers = $statistic->getCountStatusAdvertisrs($active);
        $this->view->countActiveAdvertisers = $countActiveAdvertisers;
        
        $countNotConfirmedAdvertisers = $statistic->getCountStatusAdvertisrs($notConfirmed);
        $this->view->countNotConfirmedAdvertisers = $countNotConfirmedAdvertisers;
        
        $countBlockedAdvertisers = $statistic->getCountStatusAdvertisrs($blocked);
        $this->view->countBlockedAdvertisers = $countBlockedAdvertisers;                
        
        /**
         * Advertisements
         */
        $banner = 1; $context = 2; $blog = 3; $socialNetwork = 4;
        $video = 5;
        $countAdvertisements = $statistic->getCountAllAdvertisements();
        $this->view->countAdvertisements = $countAdvertisements;
        
        $countBannerAdvertisements = $statistic->getCountTypeAdvertisements($banner);
        $this->view->countBannerAdvertisements = $countBannerAdvertisements;
        
        $countContextAdvertisements = $statistic->getCountTypeAdvertisements($context);
        $this->view->countContextAdvertisements = $countContextAdvertisements;
        
        $countBlogAdvertisements = $statistic->getCountTypeAdvertisements($blog);
        $this->view->countBlogAdvertisements = $countBlogAdvertisements;
        
        $countSocialNetworkAdvertisements = $statistic->getCountTypeAdvertisements($socialNetwork);
        $this->view->countSocialNetworkAdvertisements = $countSocialNetworkAdvertisements;
        
        $countVideoAdvertisements = $statistic->getCountTypeAdvertisements($video);
        $this->view->countVideoAdvertisements = $countVideoAdvertisements;
        
        /**
         * Campaigns
         */
        $countCampaigns = $statistic->getCountAllCampaigns();
        $this->view->countCampaigns = $countCampaigns;
        
        $countActiveCampaigns = $statistic->getCountStatusCampaigns($active);
        $this->view->countActiveCampaigns = $countActiveCampaigns;
        
        $countNotConfirmedCampaigns = $statistic->getCountStatusCampaigns($notConfirmed);
        $this->view->countNotConfirmedCampaigns = $countNotConfirmedCampaigns;
        
        $countBlockedCampaigns = $statistic->getCountStatusCampaigns($blocked);
        $this->view->countBlockedCampaigns = $countBlockedCampaigns;
        
        /**
         * Publishers
         */
        $countPublishers = $statistic->getCountAllPublishers();
        $this->view->countPublishers = $countPublishers;
        
        $countActivePublishers = $statistic->getCountStatusPublishers($active);
        $this->view->countActivePublishers = $countActivePublishers;
        
        $countNotConfirmedPublishers = $statistic->getCountStatusPublishers($notConfirmed);
        $this->view->countNotConfirmedublishers = $countNotConfirmedPublishers;
        
        $countBlockedPublishers = $statistic->getCountStatusPublishers($blocked);
        $this->view->countBlockedPublishers = $countBlockedPublishers;
        
        /**
         * Publisher websites
         */
        $countSites = $statistic->getCountAllSites();
        $this->view->countSites = $countSites;
        
        $countActiveSites = $statistic->getCountStatusSites($active);
        $this->view->countActiveSites = $countActiveSites;
        
        $countNotConfirmedSites = $statistic->getCountStatusSites($notConfirmed);
        $this->view->countNotConfirmedSites = $countNotConfirmedSites;
        
        $countBlockedSites = $statistic->getCountStatusSites($blocked);
        $this->view->countBlockedSites = $countBlockedSites;
        
        /**
         * Advertising spaces
         */
        $countAdvSpaces = $statistic->getCountAllAdvSpaces();
        $this->view->countAdvSpaces = $countAdvSpaces;
        
        $countActiveAdvSpaces = $statistic->getCountStatusAdvSpaces($active);
        $this->view->countActiveAdvSpaces = $countActiveAdvSpaces;
        
        $countNotConfirmedAdvSpaces = $statistic->getCountStatusAdvSpaces($notConfirmed);
        $this->view->countNotConfirmedAdvSpaces = $countNotConfirmedAdvSpaces;
        
        $countBlockedAdvSpaces = $statistic->getCountStatusAdvSpaces($blocked);
        $this->view->countBlockedAdvSpaces = $countBlockedAdvSpaces;
        
        /**
         * Balance
         */
        $advertisersBalance = $statistic->getAdvertisersBalance();
        $this->view->advertisersBalance = $advertisersBalance;
        
        $publishersBalance = $statistic->getPublishersBalance();
        $this->view->publishersBalance = $publishersBalance;
        
        $totalBalance = $advertisersBalance['balance'] + $publishersBalance['balance'];
        $this->view->totalBalance = round($totalBalance, 2);
    }

    /**
     * Вывод всех рекламодателей
     */
    public function advertisersAction() {
        $obj = new Back_Model_Admin();
        $all = $obj->getAllAdvers();
        $this->view->all = $all;
    }

    /**
     * Блокировка выбранного рекламодателя
     */
    public function blockadverAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $data = array(
            'id_status' => 3
        );
        $obj->blockSelectAdver($id, $data);
        $this->_helper->redirector('advertisers', 'admin');
    }

    /**
     * Подтверждение регистрации выбранного
     * рекламодателя
     */
    public function confirmadverAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $data = array(
            'id_status' => 1
        );
        $obj->confirmSelectAdver($id, $data);
        $this->_helper->redirector('advertisers', 'admin');
    }

    /**
     * Разблокирование выбранного рекламодателя
     */
    public function unblockadverAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $data = array(
            'id_status' => 1
        );
        $obj->unblockSelectAdver($id, $data);
        $this->_helper->redirector('advertisers', 'admin');
    }

    /**
     * Удаление выбранного рекламодателя
     */
    public function deleteadverAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $obj->deleteSelectAdver($id);
        $this->_helper->redirector('advertisers', 'admin');
    }

    /**
     * Выборка данных для профиля 
     * указанного рекламодателя
     */
    public function advertiserAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $user = $obj->getSelectAdver($id);
        $this->view->user = $user;
    }

    /**
     * вывод всех публикаторов
     */
    public function publishersAction() {
        $obj = new Back_Model_Admin();
        $all = $obj->getAllPublishers();
        $this->view->all = $all;
    }
    
    /**
     * блокировка выбранного публикатора 
     */
    public function blockpublisherAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        //Проверяем с какой страницы прищол запрос
        $key = $this->_getParam('key');
        $data = array(
            'id_status' => 3
        );
        $obj->blockSelectPublisher($id, $data);
        if ($key == 1) {
            $this->getHelper('Redirector')->gotoSimpleAndExit('publisher', 'admin', 'back', array('id' => $id));
        } else {
            $this->_helper->redirector('publishers', 'admin');
        }
    }
    /**
     * подтверждение регистрайии выбранного публикатора 
     */
    public function confirmpublisherAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $data = array(
            'id_status' => 1
        );
        $obj->confirmSelectPublisher($id, $data);
        $this->_helper->redirector('publishers', 'admin');
    }
    
    /**
     * розблокирование вырбраного публикатора 
     */

    public function unblockpublisherAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        //Проверяем с какой страницы прищол запрос
        $key = $this->_getParam('key');
        $data = array(
            'id_status' => 1
        );
        $obj->unblockSelectPublisher($id, $data);
        if ($key == 1) {
            $this->getHelper('Redirector')->gotoSimpleAndExit('publisher', 'admin', 'back', array('id' => $id));
        } else {
            $this->_helper->redirector('publishers', 'admin');
        }
    }
    /**
     * удаление выбраного публикатора 
     */
    public function deletepublisherAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $obj->deleteSelectPublisher($id);
        $this->_helper->redirector('publishers', 'admin');
    }
    /*
     * информация о выбранном публикаторе
     */
    public function publisherAction() {
        $obj = new Back_Model_Admin();
        $id = $this->_getParam('id');
        $user = $obj->getSelectPublisher($id);
        $this->view->user = $user;
    }

    /**
     * вывод рекламодателей и публикаторов, ожидающих
     * подтверждение регистрации
     */
    public function pendingAction() {
        $obj = new Back_Model_Admin();
        //делаем выборку из таблиц 'advertisers' & 'publishers'
        $advers = $obj->getPendingAdvers();        
        $pubs = $obj->getPendingPubs();
        //мержим 2 массива и передаем в вид
        $all = array_merge($advers, $pubs);
        $this->view->all = $all;
    }
    
    public function websitesAction () {
        $obj = new Back_Model_Websites();
        
        $site = $obj->getAllWebSites();
        
        //подсчитываем общее количество сайтов
        $countObj = new Back_Model_Statistic();
        $allSite = $countObj->getCountAllSites();
        //подсчет всех рекламных мест
        $allAdvSpaces = $countObj->getCountAllAdvSpaces();
        //подсчет активных сайтов
        $active = $countObj->getCountStatusSites(1);
        //подсчет заблокированых сайтов
        $blocked = $countObj->getCountStatusSites(3);
        
        $this->view->active = $active;
        $this->view->blocked = $blocked;
        $this->view->allsites = $allSite;
        $this->view->adspaces = $allAdvSpaces; 
        $this->view->websites = $site;
        
    }
    
    public function activatesiteAction() {
        $obj = new Back_Model_Websites();
        $id_site = $this->_getParam('id');
        
        $data = array(
            'id_status' => 1
        );
        $obj->activateBlockedWebsite($data, $id_site);
        $this->_helper->redirector('websites', 'admin');
    }
    
    public function blocksiteAction() {
        $obj = new Back_Model_Websites();
        $id_site = $this->_getParam('id');
        
        $data = array(
            'id_status' => 3
        );
        $obj->activateBlockedWebsite($data, $id_site);
        $this->_helper->redirector('websites', 'admin');
    }        

}

?>
